package com.zrrd.controller;


import com.zrrd.common.dto.SetRulesDto;
import com.zrrd.common.page.RolePageVo;
import com.zrrd.entity.Role;
import com.zrrd.entity.RoleRule;
import com.zrrd.service.RoleRuleService;
import com.zrrd.service.RoleService;
import com.zrrd.common.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-09-05
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private RoleRuleService roleRuleService;

    @ApiOperation(value = "分页查询角色列表",notes = "分析查询角色列表")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") Integer page,
                                     @RequestParam(value = "limit", required = false) Integer limit ){
        RolePageVo rolePageVo=roleService.findByPage(page,limit);
        return ResponseResult.queryResponse(rolePageVo);
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @PostMapping()
    public ResponseResult add(@RequestBody Role role){    //如果参数是某个实体类，那么注意在参数类型前加@RequestBody注解
        return ResponseResult.saveResponse(roleService.save(role));  //新增，把对象里的属性，新增到MySQL数据库
    }

    /**
     * 修改
     * @param role
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody Role role){
        return ResponseResult.updateResponse(roleService.updateById(role)); //修改，根据id修改数据库的内容
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}/delete")
    public ResponseResult delete(@PathVariable("id") Integer id){ //如果参数类型不是实体类，而是其他如整数等类型，那么添加@PathVariable 注解
        return ResponseResult.deleteResponse(roleService.removeById(id)); //删除，根据id删除数据库的数据
    }

    //修改状态
    @PutMapping("/{id}/updateStatus")
    public ResponseResult updateStatus(@PathVariable("id")Integer id,@RequestBody Role role){
        boolean flag = roleService.lambdaUpdate()
                .eq(Role::getId, id)
                .set(Role::getStatus, role.getStatus())
                .update();

        return ResponseResult.updateResponse(flag);
    }

    //设置角色权限
    @PostMapping("/setRules")
    public ResponseResult setRules(@RequestBody SetRulesDto setRulesDto){
        //1.获取角色id
        Integer roleId = setRulesDto.getId();
        boolean flag=false;
        //2.遍历权限菜单列表
        for (Integer ruleId : setRulesDto.getRuleIds()) {
            RoleRule rr = roleRuleService.lambdaQuery()
                    .eq(RoleRule::getRoleId, roleId)
                    .eq(RoleRule::getRuleId, ruleId)
                    .one();
            if (rr != null) continue;
            RoleRule roleRule = new RoleRule();
            roleRule.setRoleId(roleId);
            roleRule.setRuleId(ruleId);
            flag = roleRuleService.save(roleRule);
        }
        return ResponseResult.updateResponse(flag);
    }

}

